//
//  YZEMySuifangTableViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/6/24.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "YZEMySuifangTableViewController.h"
#import "CommonDefine.h"
#import "RPGrafting.h"
#import "UITableView+EmptyData.h"
#import "YZESuifangGanzangTableViewController.h"
#import "YZESuifangShenzangTableViewController.h"
#import "YZEGanzangCheckViewController.h"
#import "YZEShenzangCheckViewController.h"
#import "YZEAddSuifangViewController.h"

#define URL_Getmysuifang @"/api/sickpeople/suifang"
#define URL_DeleteSuifang @"/api/sickpeople/suifang/"

@interface YZEMySuifangTableViewController ()
@property(nonatomic,assign) int pagenum;//页号
@property(nonatomic,strong) NSMutableArray *suifangArr;

@property(nonatomic,strong) NSDictionary *dataNameDic;
@end

@implementation YZEMySuifangTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的随访";
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.automaticallyAdjustsScrollViewInsets = YES;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"新增"
                                                                             style:UIBarButtonItemStyleDone
                                                                            target:self
                                                                            action:@selector(addAction)];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.pagenum = 0;
    
    [[YZENetwork sharedInstace]getRequestWithUrl:[NSString stringWithFormat:@"%@%@?page=%d&size=%d",BASE,URL_Getmysuifang,_pagenum,PAGESIZE]
                                      parameters:nil
                                        delegate:self
                                      requestTag:1
                                    successBlock:^(id responseObject)
    {
        self.suifangArr = [RPGrafting mj_objectArrayWithKeyValuesArrayAdd:responseObject];
        [self.tableView reloadData];
        [self initRefersh];
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 业务逻辑
-(void)initRefersh{
    __block YZEMySuifangTableViewController *suifangVC = self;
    self.tableView.mj_header=[RefershHelper headerWithRefreshingBlock:^{
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        suifangVC.pagenum++;
    }];
    self.pagenum = 0;
    self.tableView.mj_footer=[RefershHelper backFooterWithRefreshingBlock:^{
        suifangVC.pagenum++;
        [[YZENetwork sharedInstace]getRequestWithUrl:[NSString stringWithFormat:@"%@%@?page=%d&size=%d",BASE,URL_Getmysuifang,suifangVC.pagenum,PAGESIZE] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
            NSMutableArray *dataArr = [RPGrafting mj_objectArrayWithKeyValuesArrayAdd:responseObject];
            if (!dataArr || dataArr.count==0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                [self.suifangArr addObjectsFromArray:dataArr];
                [self.tableView reloadData];
            }
        }];
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(NSString*)convertSuifangNameFromData:(NSString*)dataName{
    if ([self.dataNameDic objectForKey:dataName]) {
        return [self.dataNameDic objectForKey:dataName];
    }
    return dataName;
}

-(void)sendDeleteSuifangReq:(NSIndexPath*)indexpath{
    RPGrafting *grafting = self.suifangArr[indexpath.row];
    [[YZENetwork sharedInstace]deleteRequestWithUrl:[NSString stringWithFormat:@"%@%@%@/%ld", BASE, URL_DeleteSuifang, grafting.name, grafting.id]
                                         parameters:nil
                                           delegate:self
                                         requestTag:1
                                       successBlock:^(id responseObject)
    {
        [self.suifangArr removeObjectAtIndex:indexpath.row];
        [self.tableView reloadData];
    }];
}


#pragma mark - 事件响应
-(void)addAction{
    YZEAddSuifangViewController *v = [YZEAddSuifangViewController new];
    [self.navigationController pushViewController:v animated:YES];
}

#pragma mark - Table view data source

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *a1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        // 先处理数据
        [self sendDeleteSuifangReq:indexPath];
    }];
    
    return @[a1];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithPicAndMsg:@"暂无内容" ifNecessaryForRowCount:self.suifangArr.count];
    return self.suifangArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    RPGrafting *grafting = self.suifangArr[indexPath.row];
    cell.textLabel.text = [self convertSuifangNameFromData:grafting.name];
    cell.detailTextLabel.text = grafting.createdDate;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIViewController *vc;
    RPGrafting *grafting = self.suifangArr[indexPath.row];
    if ([grafting.name rangeOfString:@"Check"].location!=NSNotFound) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"suifangAndCheck" bundle:[NSBundle mainBundle]];
        if ([grafting.name rangeOfString:@"LiverGrafting"].location!=NSNotFound) {
            vc = [sb instantiateViewControllerWithIdentifier:@"liverCheckVC"];
            ((YZEGanzangCheckViewController*)vc).graftingId = grafting.id;
        }
        else if ([grafting.name rangeOfString:@"RenalGrafting"].location!=NSNotFound){
            vc = [sb instantiateViewControllerWithIdentifier:@"renalCheckVC"];
            ((YZEShenzangCheckViewController*)vc).graftingId = grafting.id;
        }
    }
    else {
        if ([grafting.name rangeOfString:@"LiverGrafting"].location!=NSNotFound) {
            vc = [YZESuifangGanzangTableViewController new];
            ((YZESuifangGanzangTableViewController *)vc).suifangId = grafting.id;
        }
        else if ([grafting.name rangeOfString:@"RenalGrafting"].location!=NSNotFound){
            vc = [YZESuifangShenzangTableViewController new];
            ((YZESuifangShenzangTableViewController *)vc).suifangId = grafting.id;
        }
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 数据配置
-(NSDictionary*)dataNameDic{
    if (!_dataNameDic) {
        _dataNameDic = @{
                         @"RenalGrafting":@"肾移植随访",
                         @"RenalGraftingCheck":@"肾移植检查",
                         @"LiverGrafting":@"肝移植随访",
                         @"LiverGraftingCheck":@"肝移植检查",
                         @"LungGrafting":@"肺移植随访",
                         @"LungGraftingCheck":@"肺移植检查"
                         };
    }
    return _dataNameDic;
}

@end
